import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Main from "../views/Main";

// 固定有的路由
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('@/views/Login'),
    meta: { title: '登录' },
    hidden: true
  },
  { 
    path: '/404', 
    component: () => import('@/views/404'), 
    meta: { title: '404' },
    hidden: true 
  },
  {
    path: '/',
    component: Main,
    redirect:'/main',
    name: '后台首页',
    meta: { title: '后台首页', icon: 'el-icon-location'  }, // 判断是否需要登录 requireAuth: true
    children:[
      {
        path:'/main',
        name:'后台首页',
        component: ()=>import('@/views/Home'),
        meta: { title: '后台首页', icon: 'el-icon-location' },
      }
    ]
  },
]

//权限路由
export const asyncRoutes = [
  // 讲师管理
  {
    path: '/teacher',
    component: Main,
    redirect: '/teacher/table',
    name: '讲师管理',
    meta: { title: '讲师管理', icon: 'el-icon-s-marketing' },
    children: [
      {
        path: '/teacher/table',
        name: '讲师列表',
        component: () => import('@/views/edu/teacher/list'),
        meta: { title: '讲师列表', icon: 'el-icon-s-marketing' }
      },
      {
        path: '/teacher/save',
        name: '添加讲师',
        component: () => import('@/views/edu/teacher/save'),
        meta: { title: '添加讲师', icon: 'el-icon-s-marketing' }
      },
      {
        path: '/teacher/edit/:id',
        name: '编辑讲师',
        component: () => import('@/views/edu/teacher/save'),
        meta: { title: '编辑讲师', noCache: true },
        hidden: true
      }
    ]
  },
  {
    path: '/subject',
    component: Main,
    redirect: '/subject/list',
    name: '课程分类管理',
    meta: { title: '课程分类管理', icon: 'el-icon-s-flag' },
    children: [
      {
        path: '/subject/list',
        name: '课程分类列表',
        component: () => import('@/views/edu/subject/list'),
        meta: { title: '课程分类列表', icon: 'el-icon-s-flag' }
      },
      {
        path: '/subject/save',
        name: '添加课程分类',  
        component: () => import('@/views/edu/subject/save'),
        meta: { title: '添加课程分类', icon: 'el-icon-s-flag' }
      }
    ]
  },

  {
    path: '/course',
    component: Main,
    redirect: '/course/list',
    name: '课程管理',
    meta: { title: '课程管理', icon: 'el-icon-s-finance' },
    children: [
      {
        path: '/course/list',
        name: '课程列表',
        component: () => import('@/views/edu/course/list'),
        meta: { title: '课程列表', icon: 'el-icon-s-finance' }
      },
      {
        path: '/course/info',
        name: '添加课程',  
        component: () => import('@/views/edu/course/info'),
        meta: { title: '添加课程', icon: 'el-icon-s-finance' }
      },
      {
        path: '/course/info/:id',
        name: '编辑课程基本信息',
        component: () => import('@/views/edu/course/info'),
        meta: { title: '编辑课程基本信息', noCache: true },
        hidden: true
      },
      {
        path: '/course/chapter/:id',
        name: '编辑课程大纲',
        component: () => import('@/views/edu/course/chapter'),
        meta: { title: '编辑课程大纲', noCache: true },
        hidden: true
      },
      {
        path: '/course/publish/:id',
        name: '发布课程',
        component: () => import('@/views/edu/course/publish'),
        meta: { title: '发布课程', noCache: true },
        hidden: true
      }
    ]
  },
  {
    path: '/sta',
    component: Main,
    redirect: '/sta/create',
    name: '统计分析',
    meta: { title: '统计分析', icon: 'el-icon-s-management' },
    children: [
      {
        path: '/sta/create',
        name: '生成数据',
        component: () => import('@/views/sta/create'),
        meta: { title: '生成数据', icon: 'el-icon-s-management' }
      },
      {
        path: '/sta/show',
        name: '图表显示',
        component: () => import('@/views/sta/show'),
        meta: { title: '图表显示', icon: 'el-icon-s-management' }
      }
    ]
  },

  {
    path: '/acl',
    component: Main,
    redirect: '/acl/user/list',
    name: '权限管理',
    meta: { title: '权限管理', icon: 'el-icon-s-management' },
    children: [
      {
        path: '/acl/user/list',
        name: '用户管理',
        component: () => import('@/views/acl/user/list'),
        meta: { title: '用户管理' }
      },
      {
        path: '/acl/role/list',
        name: '角色管理',
        component: () => import('@/views/acl/role/list'),
        meta: { title: '角色管理' }
      },
      {
        path: '/acl/role/form',
        name: '角色添加',
        component: () => import('@/views/acl/role/form'),
        meta: { title: '角色添加' },
        hidden: true
      },
      {
        path: '/acl/role/update/:id',
        name: '角色修改',
        component: () => import('@/views/acl/role/form'),
        meta: { title: '角色修改' },
        hidden: true
      },
      {
        path: '/acl/role/distribution/:id',
        name: '角色权限',
        component: () => import('@/views/acl/role/roleForm'),
        meta: { title: '角色权限' },
        hidden: true
      },
      {
        path: '/acl/menu/list',
        name: '菜单管理',
        component: () => import('@/views/acl/menu/list'),
        meta: { title: '菜单管理' }
      },
      {
        path: '/acl/user/add',
        name: '用户添加',
        component: () => import('@/views/acl/user/form'),
        meta: { title: '用户添加' },
        hidden: true
      },
      {
        path: '/acl/user/update/:id',
        name: '用户修改',
        component: () => import('@/views/acl/user/form'),
        meta: { title: '用户修改' },
        hidden: true
      },
      {
        path: '/acl/user/role/:id',
        name: '用户角色',
        component: () => import('@/views/acl/user/roleForm'),
        meta: { title: '用户角色' },
        hidden: true
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

//得到所有的权限路由，然后给予添加到数据库   --- 作用于把路由添加到数据库里
export function addRouter(){
  return asyncRoutes;
}

// 重新匹配路由方法
export function resetRouter(dynamicRoutes) { 
  //如果左侧的菜单已经大于三了，可能是重复执行
  if(constantRoutes.length>3){
    return;
  }
  
  // 如果用户的权限为null或者没有，则直接返回
  if(dynamicRoutes==null||dynamicRoutes.length==0){
    return null;
  }
  // 返回的集合
  let list = [];

  for(let i = 0;i<asyncRoutes.length;i++){
    // 最后一个是404 页面，所有用户都拥有
    if(i==asyncRoutes.length-1){
      if(constantRoutes.indexOf(asyncRoutes[i])==-1){
        list.push(asyncRoutes[i]);
      }
      constantRoutes.push(asyncRoutes[i]);
      break;
    }
    //双重循环遍历权限，找到path相同的权限，则添加到list中
    for(let j = 0;j<dynamicRoutes.length;j++){
      if(asyncRoutes[i].path==dynamicRoutes[j].path){
        
        // 添加到list的路由，用于左侧菜单展示
        let routerVo = clone(asyncRoutes[i]); // 这里objOld是你想要复制的对象
        if(constantRoutes.indexOf(routerVo)==-1){
          constantRoutes.push(routerVo);
        }

        // 判断是否有子菜单
        if(asyncRoutes[i].children!=null&&asyncRoutes[i].children.length>0){
          // 存在子菜单的话，需要将隐藏的不显示
          routerVo.children = childrenRouter(dynamicRoutes[j].children,asyncRoutes[i].children);
        }

        // 添加到list中，返回给添加到路由中
        let routerList = clone(asyncRoutes[i]); // 这里objOld是你想要复制的对象
        list.push(routerList);
        // 判断哪些子菜单用户拥有该权限
        if(asyncRoutes[i].children!=null&&asyncRoutes[i].children.length>0){
          routerList.children = childrenPermission(dynamicRoutes[j].children,asyncRoutes[i].children)
        }
      }
    }
  }
  return list;
}

// 复制对象给变量
function clone(myObj){
	if(typeof(myObj) != 'object'){
		return myObj;
	}
	if(myObj == null){
		return myObj;
	}
	if(myObj instanceof Array){
		var myNewObj = new Array();
	}else{
		var myNewObj = new Object();	
	}
	for(var i in myObj){
		myNewObj[i] = clone(myObj[i]);
	}
	return myNewObj;
};


// 添加子菜单  == 不添加隐藏的子菜单
function childrenRouter(permission,routes){
  let list = [];
  for(let i = 0;i<permission.length;i++){
    for(let j = 0;j<routes.length;j++){
      if(permission[i].path==routes[j].path){
        if(routes[j].hidden!=true){
          list.push(routes[j]);
        }
      }
    }
  }
  return list;
}


// 添加子菜单权限  == 拥有权限的才添加
function childrenPermission(permission,routes){
  let list = [];
  for(let i = 0;i<permission.length;i++){
    for(let j = 0;j<routes.length;j++){
      if(permission[i].path==routes[j].path){
        list.push(routes[j]);
      }
    }
  }
  return list;
}

export default new Router({
  //地址栏中去掉#符号
  mode:'history', //history模式，地址栏中去掉#符号
  scrollBehavior: () => ({ y: 0 }), //滚动行为
  routes: constantRoutes
})
